<template>
  <div id="app">
    <router-view />
  </div>
</template>

<script>
export default {
  name: 'App',
  data() {
    return {
      timecheck: ''
    }
  },
  computed: {
    user() {
      return this.$store.getters.user
    }
  },
  watch: {
    'user': function(newVal, oldVal) {
      if (newVal) {
        // 表明已经登陆了
        clearInterval(this.timecheck)
        this.timecheck = ''
        this.timecheck = setInterval(() => {
          this.checkEnabled()
        }, 15000)
      } else {
        // 没有登陆
        clearInterval(this.timecheck)
        this.timecheck = ''
      }
    }
  },
  methods: {
    checkEnabled() {
      this.$store.dispatch('user/getInfo').then(res => {
        // console.log(res)
        // eslint-disable-next-line eqeqeq
        if (res.enabled == 0) {
          // 清除登陆状态，清除用户数据，取消定时器，重定向到首页
          this.$message.error('您的账号已被禁用，自动退出')
          // common.userMessageInit()
          clearInterval(this.timecheck)
          this.timecheck = ''
          this.$store.dispatch('user/logout').then(res => {
            this.$router.push('/lr/login')
          })
        }
      })
    }
  }
}
</script>
